package com.qys.livesMall.product.controller.sys;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.product.entity.H2ProductSkuAttribute;
import com.qys.livesMall.product.entity.ProductSkuAttributeOptions;
import com.qys.livesMall.product.model.H2ProductSkuAttributeDTO;
import com.qys.livesMall.product.service.H2ProductSkuAttributeService;
import com.qys.livesMall.product.service.ProductSkuAttributeOptionsService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  sku属性
 * @author gmx
 * @since 2023-05-17
 */
@RestController
@RequestMapping("/h2-shop-sku-attribute")
public class H2ProductSkuAttributeController {
    @Resource
    H2ProductSkuAttributeService skuAttributeService;

    @Resource
    ProductSkuAttributeOptionsService optionsService;
    /***
     * 根据分类ID查询
     */
    @GetMapping(value = "/category/{id}")
    public R<List<H2ProductSkuAttribute>> categoryAttributeList(@PathVariable(value = "id") Long id) {
//根据分类ID查询属性参数
        List<H2ProductSkuAttribute> skuAttributes = skuAttributeService.queryList(id);
        return R.ok(skuAttributes);
    }
    /***
     * 插入sku属性或者修改属性
     */
    @PostMapping(value ="/save")
    public R<H2ProductSkuAttribute> save(@RequestBody H2ProductSkuAttribute h2ProductSkuAttribute){
        if (ObjectUtils.isEmpty(h2ProductSkuAttribute.getId())){
            boolean b = skuAttributeService.save(h2ProductSkuAttribute);
            if(!b){
                return R.fail("插入sku属性失败");
            }
            return R.ok(h2ProductSkuAttribute);
        }
        boolean b = skuAttributeService.updateById(h2ProductSkuAttribute);
        if(!b){
            return R.fail("修改sku属性失败");
        }
        return R.ok();
    }

    /***
     * 修改属性
     */
    @PostMapping(value ="/edit")
    public R edit(@RequestBody H2ProductSkuAttributeDTO dto){
        H2ProductSkuAttribute h2ProductSkuAttribute=new H2ProductSkuAttribute();
        BeanUtils.copyProperties(dto,h2ProductSkuAttribute);
        boolean b = skuAttributeService.updateById(h2ProductSkuAttribute);
        if(!b){
            return R.fail("修改sku属性失败");
        }
        return R.ok();
    }

    @PostMapping(value = "/saveOption")
    public R<ProductSkuAttributeOptions> saveOption(@RequestBody ProductSkuAttributeOptions options){
            optionsService.saveOrUpdate(options);
        return optionsService.saveOrUpdate(options)?R.ok(options):R.fail();
    }
    /***
     * 删除
     */
    @DeleteMapping(value = "/{id}")
    public R delete(@PathVariable Long id){
        boolean i = skuAttributeService.removeById(id);
        if(!i){
            return R.fail("删除失败");
        }
        return R.ok();
    }
    @DeleteMapping(value = "deleteOption/{id}")
    public R deleteOption(@PathVariable Long id){
        boolean i = optionsService.removeById(id);
        if(!i){
            return R.fail("删除失败");
        }
        return R.ok();
    }
}
